import { base_url } from './config.js';
import { getToken } from '../util/tool.js';

export const checkin = {
	// 专注模式打卡
	async focusCheckIn(duration) {
		let jwt = getToken();
		return new Promise((resolve, reject) => {
			uni.request({
				url: base_url + '/checkin/focusCheckIn',
				method: 'POST',
				header: {
					'content-type': 'application/json',
					'Authorization': jwt
				},
				data: { duration },
				success(res) {
					console.log('【 Response in request focusCheckIn 】', res);
					resolve(res.data);
				},
				fail(err) {
					console.log('【 Error in request focusCheckIn 】', err);
					reject({ success: false });
				}
			});
		});
	},
	// 打卡记录统计接口
	async checkinStatistics() {
		let jwt = getToken();
		return new Promise((resolve, reject) => {
			uni.request({
				url: base_url + '/checkin/checkinStatistics',
				method: 'GET',
				header: {
					'content-type': 'application/json',
					'Authorization': jwt
				},
				success(res) {
					console.log('【 Response in request checkinStatistics 】', res);
					resolve(res.data);
				},
				fail(err) {
					console.log('【 Error in request checkinStatistics 】', err);
					reject({ success: false });
				}
			});
		});
	},
	// 获取专注打卡排行榜接口
	async checkinRanking() {
		let jwt = getToken();
		return new Promise((resolve, reject) => {
			uni.request({
				url: base_url + '/checkin/checkinRanking',
				method: 'GET',
				header: {
					'content-type': 'application/json',
					'Authorization': jwt
				},
				success(res) {
					console.log('【 Response in request checkinRanking 】', res);
					resolve(res.data);
				},
				fail(err) {
					console.log('【 Error in request checkinRanking 】', err);
					reject({ success: false });
				}
			});
		});
	},
	// 统计最近7天的打卡趋势接口
	async checkinTrend() {
		let jwt = getToken();
		return new Promise((resolve, reject) => {
			uni.request({
				url: base_url + '/checkin/checkinTrend',
				method: 'GET',
				header: {
					'content-type': 'application/json',
					'Authorization': jwt
				},
				success(res) {
					console.log('【 Response in request checkinTrend 】', res);
					resolve(res.data);
				},
				fail(err) {
					console.log('【 Error in request checkinTrend 】', err);
					reject({ success: false });
				}
			});
		});
	},
	// 获取主页的用户打卡统计信息
	async getMainPageNeedInfo() {
		let jwt = getToken();
		return new Promise((resolve, reject) => {
			uni.request({
				url: base_url + '/checkin/getMainPageNeedInfo',
				method: 'GET',
				header: {
					'content-type': 'application/json',
					'Authorization': jwt
				},
				success(res) {
					console.log('【 Response in request getMainPageNeedInfo 】', res);
					resolve(res.data);
				},
				fail(err) {
					console.log('【 Error in request getMainPageNeedInfo 】', err);
					reject({ success: false });
				}
			});
		});
	}
};